/* internal/js/food-item/form.js */

function showHideInputBox () {
	var opt = document.getElementById('foodItemType');
	var val = opt.options[opt.selectedIndex].value;
	var tb = document.getElementById('foodItemNewType');
	if (val == 'new') {
		tb.style.display = 'inline';
		tb.focus();
	} else {
		tb.style.display = 'none';
		tb.value = '';
	}
}

function resetFields () {
	document.getElementById('foodItemId').value = '';
	document.getElementById('foodItemName').value = '';
	document.getElementById('foodItemDescription').value = '';
	document.getElementById('foodItemCost').value = '';
	document.getElementById('foodItemType').selectedIndex = '0';
	showHideInputBox();
	var availRadio = document.getElementById('foodItemAvailable').elements['availRadio'];
	availRadio[0].checked = true;
	selectResultRow(0);
}

function startResultsTable () {
	var html = '<table class="searchResults">' +
	           '<tr>' +
	           '<th>Name</th>' +
	           '<th>Description</th>' +
	           '</tr>';
	return html;
}

function addResultRow (id, name, description, cost, type, available) {
	var html =
		'<form id="listItemForm' + id + '">' +
		'<input id="name" value="' + name + '" type="hidden" />' +
		'<input id="description" value="' + description + '" type="hidden" />' +
		'<input id="cost" value="' + cost + '" type="hidden" />' +
		'<input id="type" value="' + type + '" type="hidden" />' +
		'<input id="available" value="' + available + '" type="hidden" />' +
		'</form>' +
		'<tr id="listItemRow' + id + '" class="searchResultsRow" onclick="showResult(\'' + id + '\')">' +
		'<td>' + name + '</td>' +
		'<td>' + description + '</td>' +
		'</tr>';
	return html;
}

function endResultsTable (ids) {
	var html =
		'<input id="resultsIds" value="' + ids.join() +'" type="hidden" />' + 
		'</table>';
	return html;
}

function selectResultRow (id) {
	if (document.getElementById('resultsIds') != null) {
		var ids = document.getElementById('resultsIds').value.split(',');
		var count = ids.length;
		for (var loop = 0; loop < count; loop++) {
			document.getElementById('listItemRow' + ids[loop]).className = 'searchResultsRow';
		}
		try {
			document.getElementById('listItemRow' + id).className = 'selectedResultsRow';
		} catch (e) {
		}
	}
}

function showResult (id) {
	selectResultRow(id);
	
	var frm = document.getElementById('listItemForm' + id);
	
	document.getElementById('foodItemId').value = id;
	document.getElementById('foodItemName').value = frm.name.value;
	document.getElementById('foodItemDescription').value = frm.description.value;
	document.getElementById('foodItemCost').value = frm.cost.value;
	
	var typeList = document.getElementById('foodItemType');
	var type = frm.type.value;
	for (var loop = 0; loop < typeList.length; loop++) {
		if (typeList.options[loop].value == type) {
			typeList.selectedIndex = loop;
			break;
		}
	}
	showHideInputBox();
	
	var availRadio = document.getElementById('foodItemAvailable').elements['availRadio'];
	for (var i = 0; i < availRadio.length; i++) {
		//alert(availRadio[i].value);
		availRadio[i].checked = (availRadio[i].value == frm.available.value);
	}
	
	//clearResults();
}

function getCheckedValue (obj) {
	if(!obj)
		return '';
	
	var radioLength = obj.length;
	if(radioLength == undefined)
		if(obj.checked)
			return obj.value;
		else
			return '';
	
	for (var i = 0; i < radioLength; i++)
		if(obj[i].checked)
			return obj[i].value;
	return '';
}

function loadPage () {
	listFoodItemTypes();
	//listAllFoodItem();
	//show the "new" type box if needed
	showHideInputBox();
}

function buildTable (items) {
	var html = startResultsTable();
	var count = items.childNodes.length;
	var ids = new Array(count);
	for (var loop = 0; loop < count; loop++) {
		var item = items.childNodes[loop];
		var id = item.getElementsByTagName('id')[0].childNodes[0].nodeValue;
		ids[loop] = id;
		
		if (item.getElementsByTagName('name')[0].childNodes.length > 0)
			var name = item.getElementsByTagName('name')[0].childNodes[0].nodeValue;
		else
			name = '';
		if (item.getElementsByTagName('description')[0].childNodes.length > 0)
			var description = item.getElementsByTagName('description')[0].childNodes[0].nodeValue;
		else
			description = '';
		if (item.getElementsByTagName('cost')[0].childNodes.length > 0)
			var cost = item.getElementsByTagName('cost')[0].childNodes[0].nodeValue;
		else
			cost = '';
		if (item.getElementsByTagName('type')[0].childNodes.length > 0)
			var type = item.getElementsByTagName('type')[0].childNodes[0].nodeValue;
		else
			type = '';
		if (item.getElementsByTagName('available')[0].childNodes.length > 0)
			var available = item.getElementsByTagName('available')[0].childNodes[0].nodeValue;
		else
			available = '';
		html += addResultRow(id, name, description, cost, type, available);
	}
	html += endResultsTable(ids);
	return html;
}
